class CustomerTag < ActiveRecord::Base

  attr_protected []

  belongs_to :customer
  belongs_to :tag
  belongs_to :enterprise


  validates :customer_id,
    :tag_id,
    :enterprise_id,
    :presence => true

  validates :customer_id,
    :uniqueness  => {
      :scope =>  [
        :enterprise_id,
        :tag_id
      ]
    }

  default_scope lambda { where( :enterprise_id => Enterprise.current_id ) }

  after_destroy do |customer_tag|
    customer_tag.tag.customer_tags.reload
    customer_tag.tag.destroy if customer_tag.tag.customer_tags.empty?
  end

end
